<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>首页</b></el-breadcrumb-item>
        <el-breadcrumb-item>员工列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>

    <el-col :span="24" class="warp-main" v-loading="loading" element-loading-text="拼命加载中">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.name" placeholder="成员账号/姓名" style="min-width: 240px;"
                      @keyup.enter.native="handleSearch"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="showAddDialog">添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <!--列表-->
      <el-table :data="users" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button size="mini" @click="showAuthorityDialog(scope.$index,scope.row)">权限设置</el-button>
            <el-button size="mini" @click="showEditDialog(scope.$index,scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="removeEmployee(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="username" label="成员账号" width="80"></el-table-column>
        <el-table-column prop="name" label="姓名" width="120" sortable></el-table-column>
        <el-table-column prop="photo" label="头像" width="120" sortable></el-table-column>
        <el-table-column prop="ename" label="英文名" width="120" sortable></el-table-column>
        <el-table-column prop="sex" label="性别" width="120" :formatter="formatSex" sortable></el-table-column>
        <el-table-column prop="birth" label="出生年月" min-width="160" sortable></el-table-column>
        <el-table-column prop="cardId" label="身份证号" min-width="160" sortable></el-table-column>
        <el-table-column prop="telePhone" label="手机号码" min-width="160" sortable></el-table-column>
        <el-table-column prop="mail" label="邮箱地址" min-width="160" sortable></el-table-column>
        <el-table-column prop="endProbation" label="试用期到期日" min-width="160" sortable></el-table-column>
        <el-table-column prop="startContract" label="合同开始日" min-width="160" sortable></el-table-column>
        <el-table-column prop="endCOntract" label="合同到期日" min-width="160" sortable></el-table-column>
        <el-table-column prop="deptId" label="所属部门" min-width="120" sortable></el-table-column>
        <el-table-column prop="organCode" label="岗位" width="120" sortable></el-table-column>
        <el-table-column prop="createTime" label="添加时间" min-width="160" sortable></el-table-column>
        <el-table-column prop="lastLoginTime" label="最后登录" min-width="160" sortable></el-table-column>
        <el-table-column prop="on" label="是否启用" width="100" :formatter="formatIsOn"  sortable></el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                       style="float:right;">
        </el-pagination>
      </el-col>

      <!-- 添加界面 -->
      <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
        <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
          <el-form-item label="成员账号" prop="username">
            <el-input v-model="addForm.username" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="addForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="头像" prop="photo">
            <!--
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="addForm.photo" :src="addForm.photo" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            -->
            <el-input v-model="editForm.photo" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="英文名" prop="ename">
            <el-input v-model="addForm.ename" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <template>
              <el-radio v-model="addForm.sex" label="1">男</el-radio>
              <el-radio v-model="addForm.sex" label="0">女</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="出生年月" prop="birth">
            <el-date-picker type="date" v-model="addForm.birth" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="身份证号" prop="cardId">
            <el-input v-model="addForm.cardId" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="telePhone">
            <el-input v-model="addForm.telePhone" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" prop="mail">
            <el-input v-model="addForm.email"  auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="试用期到期日" prop="endProbation">
            <el-date-picker type="date" v-model="addForm.endProbation" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="合同开始日" prop="startContract">
            <el-date-picker type="date" v-model="addForm.startContract" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="合同到期日" prop="endContract">
            <el-date-picker type="date" v-model="addForm.endContract" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="是否启用" prop="on">
            <el-switch v-model="addForm.on" active-value=true
                       inactive-value=false  active-color="#13ce66" inactive-color="#ff4949" auto-complete="off"></el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>

      <!-- 编辑界面 -->
      <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
        <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
          <el-form-item label="成员账号" prop="username">
            <el-input v-model="editForm.username" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="editForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="头像" prop="photo">
            <el-input v-model="editForm.photo" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="英文名" prop="ename">
            <el-input v-model="editForm.ename" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <template>
              <el-radio v-model="editForm.sex" label="1">男</el-radio>
              <el-radio v-model="editForm.sex" label="0">女</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="出生年月" prop="birth">
            <el-date-picker type="date" v-model="addForm.birth" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="身份证号" prop="cardId">
            <el-input v-model="editForm.cardId" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="telePhone">
            <el-input v-model="editForm.telePhone" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" prop="mail">
            <el-input v-model="editForm.email"  auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="试用期到期日" prop="endProbation">
            <el-date-picker type="date" v-model="addForm.endProbation" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="合同开始日" prop="startContract">
            <el-date-picker type="date" v-model="addForm.startContract" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="合同到期日" prop="endContract">
            <el-date-picker type="date" v-model="addForm.endContract" auto-complete="off"></el-date-picker>
          </el-form-item>
          <el-form-item label="是否启用" prop="on">
            <el-switch v-model="addForm.on" active-value="1"
                       inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" auto-complete="off"></el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import API from "../../api/api_employee";

  export default {
    data() {
      return {
        filters: {
          name: ""
        },
        loading: false,
        users: [],
        depts: [],
        deptIds: [],
        total: 0,
        page: 1,
        limit: 10,
        loading: false,
        // 编辑相关数据
        editFormVisible: false,
        editFormRules: {
          username: [
            {required: true, message: "请输入用户名", trigger: "blur"}
          ],
          name: [{required: true, message: "请输入姓名", trigger: "blur"}],
          cardId:[{required: true, message: "请输入身份证号", trigger: "blur"}]
        },
        editForm: {
          username: "",
          name: "",
          photo: "",
          ename: "",
          sex: "",
          birth: "",
          cardId: "",
          telePhone: "",
          mail: "",
          endProbation: "",
          startContract: "",
          endContract: "",
          on:""
        },
        // 新增相关数据
        addFormVisible: false, //新增界面是否显示
        addLoading: false,
        addFormRules: {
          username: [
            {required: true, message: "请输入用户名", trigger: "blur"}
          ],
          name: [{required: true, message: "请输入姓名", trigger: "blur"}],
          cardId:[{required: true, message: "请输入身份证号", trigger: "blur"}]
        },
        addForm: {
          username: "",
          name: "",
          photo: "",
          ename: "",
          sex: "",
          birth: "",
          cardId: "",
          telePhone: "",
          mail: "",
          endProbation: "",
          startContract: "",
          endContract: "",
          on:""
        },
        // 权限修改
        authorFormVisible: false, //新增界面是否显示
        authorLoading: false,
        authorFormRules: {

        },
        authorForm: {

        }
      };
    },
    methods: {
      //是否启用显示转换
      formatSex: function (row, column) {
        return row.sex == '1' ? "男" : row.sex == '0' ? "女" : "未知";
      },
      formatIsOn: function (row, column) {
        return row.on == true ? "启用" : row.on == false ? "弃用" : "未知";
      },
      handleCurrentChange(val) {
        this.page = val;
        this.search();
      },
      handleSearch() {
        this.total = 0;
        this.page = 1;
        this.search();
      },
      //获取用户列表
      search: function () {
        let that = this;
        let params = {
          page: that.page,
          limit: 10,
          name: that.filters.name
        };

        that.loading = true;
        API.findList(params)
          .then(
            function (result) {
              that.loading = false;
              if (result && result.page && result.page.rows) {
                that.total = result.page.total;
                that.users = result.page.rows;
              }
            },
            function (err) {
              that.loading = false;
              that.$message.error({
                showClose: true,
                message: err.toString(),
                duration: 2000
              });
            }
          )
          .catch(function (error) {
            that.loading = false;
            that.$message.error({
              showClose: true,
              message: "请求出现异常",
              duration: 2000
            });
          });
      },
      showAddDialog: function () {
        let that = this;
        this.addFormVisible = true;
      },
      showEditDialog: function (index, row) {
        let that = this;
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      },
      showAuthorityDialog: function (index, row) {
        let that = this;
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      },
      handleAvatarSuccess(res, file) {
        this.photo = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      addSubmit: function () {
        let that = this;
        this.$refs.addForm.validate(valid => {
          if (valid) {
            that.loading = true;
            let params = Object.assign({}, this.addForm);
            params.deptIds = that.deptIds
            API.addEmployee(params).then(function (result) {
              if (0 === result.code) {
                that.loading = false;
                that.$message;
                that.$message.success({
                  showClose: true,
                  message: "新增成功",
                  duration: 2000
                });
                that.$refs["addForm"].resetFields();
                that.addFormVisible = false;
                that.search();
              } else {
                that.$message.error({
                  showClose: true,
                  message: "新增失败",
                  duration: 2000
                });
              }
            });
          }
        });
      },
      editSubmit: function () {
        let that = this;
        this.$refs.editForm.validate(valid => {
          if (valid) {
            that.loading = true;
            let params = Object.assign({}, that.editForm);
            API.editEmployee(params).then(function (result) {
              if (0 === result.code) {
                // that.loading = false;
                // that.$message;

                that.$message.success({
                  showClose: true,
                  message: "修改成功",
                  duration: 2000
                });
                that.$refs["editForm"].resetFields();
                that.editFormVisible = false;
                that.search();
              } else {
                that.$message.error({
                  showClose: true,
                  message: "修改失败",
                  duration: 2000
                });
              }
            });
          }
        });
      },
      removeEmployee: function (index, row) {
        let that = this;
        this.$confirm("确认删除该记录吗?", "提示", {type: "warning"})
          .then(() => {
            that.loading = true;
            API.removeEmployee({username: row.username})
              .then(
                function (result) {
                  that.loading = false;
                  if (result && parseInt(result.code) === 0) {
                    that.$message.success({
                      showClose: true,
                      message: "删除成功",
                      duration: 1500
                    });
                    that.search();
                  }
                },
                function (err) {
                  that.loading = false;
                  that.$message.error({
                    showClose: true,
                    message: err.toString(),
                    duration: 2000
                  });
                }
              )
              .catch(function (error) {
                that.loading = false;
                that.$message.error({
                  showClose: true,
                  message: "请求出现异常",
                  duration: 2000
                });
              });
          })
          .catch(() => {
          });
      }
    },
    mounted() {
      this.handleSearch();
    }
  };
</script>

<style scoped>

</style>
